package cn.datax.service.data.market.service.impl;

import cn.datax.commo.office.word.WordUtil;
import cn.datax.common.base.BaseServiceImpl;
import cn.datax.common.core.DataConstant;
import cn.datax.common.core.R;
import cn.datax.common.core.RedisConstant;
import cn.datax.common.exception.DataException;
import cn.datax.common.rabbitmq.config.RabbitMqConstant;
import cn.datax.common.redis.service.RedisService;
import cn.datax.common.utils.MD5Util;
import cn.datax.common.utils.MsgFormatUtil;
import cn.datax.common.utils.SecurityUtil;
import cn.datax.common.utils.ThrowableUtil;
import cn.datax.service.data.market.api.dto.DataApiDto;
import cn.datax.service.data.market.api.dto.ReqParam;
import cn.datax.service.data.market.api.dto.ResParam;
import cn.datax.service.data.market.api.dto.SqlParseDto;
import cn.datax.service.data.market.api.entity.ApiWarnRuleEntity;
import cn.datax.service.data.market.api.entity.DataApiEntity;
import cn.datax.service.data.market.api.enums.ConfigType;
import cn.datax.service.data.market.api.vo.ApiHeader;
import cn.datax.service.data.market.api.vo.SqlParseVo;
import cn.datax.service.data.market.cata.dto.CataResourceRelaDto;
import cn.datax.service.data.market.cata.entity.CataResourceRelaEntity;
import cn.datax.service.data.market.cata.entity.ResourceInfoEntity;
import cn.datax.service.data.market.cata.entity.UserResourceInfoEntity;
import cn.datax.service.data.market.dao.CataResourceRelaDao;
import cn.datax.service.data.market.dao.DataApiDao;
import cn.datax.service.data.market.mapstruct.CataResourceRelaMapper;
import cn.datax.service.data.market.mapstruct.DataApiMapper;
import cn.datax.service.data.market.service.CataResourceService;
import cn.datax.service.data.market.service.DataApiService;
import cn.datax.service.data.market.utils.SqlBuilderUtil;
import cn.datax.service.data.metadata.api.entity.MetadataAuthorizeEntity;
import cn.datax.service.data.metadata.api.entity.MetadataColumnEntity;
import cn.datax.service.data.metadata.api.entity.MetadataSourceEntity;
import cn.datax.service.data.metadata.api.entity.MetadataTableEntity;
import cn.datax.service.data.metadata.api.enums.DataLevel;
import cn.datax.service.workflow.api.dto.ProcessInstanceCreateRequest;
import cn.datax.service.workflow.api.entity.BusinessEntity;
import cn.datax.service.workflow.api.feign.FlowInstanceServiceFeign;
import cn.datax.service.workflow.api.vo.FlowInstanceVo;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.aspose.words.Document;
import com.aspose.words.MailMerge;
import com.aspose.words.net.System.Data.DataRow;
import com.aspose.words.net.System.Data.DataTable;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.JdbcNamedParameter;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.parser.SimpleNode;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitorAdapter;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.util.SelectUtils;
import net.sf.jsqlparser.util.TablesNamesFinder;
import net.sf.jsqlparser.util.deparser.ExpressionDeParser;
import net.sf.jsqlparser.util.deparser.SelectDeParser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 数据API信息表 服务实现类
 * </p>
 *
 * @author yuwei
 * @since 2020-03-31
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CataResourceServiceImpl extends BaseServiceImpl<CataResourceRelaDao, CataResourceRelaEntity> implements CataResourceService {
    @Autowired
    private CataResourceRelaDao relaDao;

    @Autowired
    private CataResourceRelaMapper relaMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveRelation(CataResourceRelaDto dto) {
        CataResourceRelaEntity entity = relaMapper.toEntity(dto);
        return  relaDao.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRelation(CataResourceRelaDto dto) {
        CataResourceRelaEntity entity = relaMapper.toEntity(dto);
        relaDao.updateById(entity);
    }


    @Override
    public List<ResourceInfoEntity> getRelationByCataId(String cataId) {

        return null;
    }
    @Override
    public List<ResourceInfoEntity> getResourceByCataId(String cataId, String resourceType) {
        Map paramMap = new HashMap();
        if(null != cataId && !"".equals(cataId))
            paramMap.put("cataId",cataId);
        if(null != resourceType && !"".equals(resourceType))
            paramMap.put("resourceType",resourceType);
        if("api".equals(resourceType)){
            List<ResourceInfoEntity> entity = relaDao.listApiResourceInfoByMap(paramMap);
            return entity;
        }else{
            List<ResourceInfoEntity> entity = relaDao.listTableResourceInfoByMap(paramMap);
            return entity;
        }
    }

    @Override
    public IPage<ResourceInfoEntity> pageGetResourceByCataId(Page page, String cataId, String resourceType, String resourceName) {
        System.out.println("########################pageGetResourceByCataId:" + resourceType);
        IPage<ResourceInfoEntity> iPage = new Page<>(page.getCurrent(), page.getSize());
        Map param = new HashMap();
        if(null != cataId && !cataId.isEmpty())
            param.put("cataId",cataId);
        if(null != resourceType && !resourceType.isEmpty())
            param.put("resourceType",resourceType);
        if(null != resourceName && !resourceName.isEmpty()){
            param.put("resource_name",resourceName);
        }
        IPage<ResourceInfoEntity> list = null;
        if("api".equals(resourceType)){
            list = relaDao.pageResourceApiResourceInfoByMap(iPage,param);
        }else if("table".equals(resourceType)){
            list = relaDao.pageResourceTableResourceInfoByMap(iPage,param);
        }
        return list;
    }

    @Override
    public int  getCataResourceCount(String cataId) {
        Map paramMap = new HashMap();
        paramMap.put("cata_id",cataId);
        int total = relaDao.getCataResourceCountByMap(paramMap);
        return total;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelationById(String id) {
        relaDao.deleteById(id);
    }
}
